<template>
    <el-card style="width: 400px; margin: 0 auto;">
        <el-form label-width="120px" size="small" :model="form" :rules="rules" ref="pass">
            <el-form-item label="原密码" prop="password">
                <el-input v-model="form.password" autocomplete="off" show-password></el-input>
            </el-form-item>
            <el-form-item label="新密码" prop="newPassword">
                <el-input v-model="form.newPassword" autocomplete="off" show-password></el-input>
            </el-form-item>
            <el-form-item label="确认新密码" prop="confirmPassword">
                <el-input v-model="form.confirmPassword" autocomplete="off" show-password></el-input>
            </el-form-item>
            <el-form-item>
                <el-button type="primary" @click="save">确 定</el-button>
            </el-form-item>
        </el-form>
    </el-card>
</template>

<script>
    export default {
        name: 'Password',
        data() {
            return {
                form: {},
                user: localStorage.getItem("user") ? JSON.parse(localStorage.getItem("user")) : {},
                rules: {
                    password: [{
                            required: true,
                            message: '请输入原密码',
                            trigger: 'blur'
                        },
                        {
                            min: 3,
                            max: 15,
                            message: '请输入长度在 3 到 15 个字符',
                            trigger: 'blur'
                        }
                    ],
                    newPassword: [{
                            required: true,
                            message: '请输入新密码',
                            trigger: 'blur'
                        },
                        {
                            min: 3,
                            max: 15,
                            message: '请输入长度在 3 到 15 个字符',
                            trigger: 'blur'
                        }
                    ],
                    confirmPassword: [{
                            required: true,
                            message: '请确认新密码',
                            trigger: 'blur'
                        },
                        {
                            min: 3,
                            max: 15,
                            message: '请输入长度在 3 到 15 个字符',
                            trigger: 'blur'
                        }
                    ],
                }
            }
        },
        created() {
            this.form.username = this.user.username
        },
        methods: {
            save() {
                this.$refs.pass.validate((valid) => {
                    if (valid) {
                        if (this.form.newPassword !== this.form.confirmPassword) {
                            this.$message.error("两次输入的密码不相同")
                            return false
                        }
                        this.request.post("/user/password", this.form).then(res => {
                            if (res.code === '200') {
                                this.$message.success("修改成功")
                                this.$router.push("/")
                                localStorage.removeItem("user")
                                // 重置路由
                                resetRouter()
                            } else {
                                this.$message.error(res.msg)
                            }
                        })
                    }
                })
            }
        }
    }
</script>

<style scoped>

</style>